package com.op.teachingliteapp.fragment.course;


import java.util.ArrayList;

import net.loonggg.bar.MainFragment;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.GridView;
import android.widget.Toast;

import com.op.teachingliteapp.R;
import com.op.teachingliteapp.activity.HomeActivity;
import com.op.teachingliteapp.activity.CourseActivity;

public class CourseFragment extends Fragment {

	 final int itemId=1;
		private GridView mGridView;
		ArrayList<CourseCellDataProvider>  list=new ArrayList<CourseCellDataProvider>();
	   private CourseGridAdapter mAdapter;
	   private boolean isShowDelete=false;
	   private int index;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_course, container,false);
		
		mGridView = (GridView)rootView.findViewById(R.id.gridView);
		mAdapter=new CourseGridAdapter(getActivity(), list);
		mGridView.setAdapter(mAdapter);
		mGridView.setOnItemLongClickListener(new OnItemLongClickListener() {

			 @Override
				public boolean onItemLongClick(AdapterView<?> parent, View view,
						final int position, long id) {
				
				 if(position==list.size()-1&&isShowDelete==false){
					 /*
					  * 这里就是点击添加课程，长按是进入有侧边索银兰的添加课程fragment。
					  */
					 HomeActivity.setBackChoice(1);//控制界面跳转的逻辑
					 getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.main_show, new MainFragment()).commit();
				 }
				 
					  if (isShowDelete) {
						    isShowDelete = false;
						   } else {
							   
						    isShowDelete = true;
						
				 }
						  mAdapter.setIsShowDelete(isShowDelete);
					return true;
			 }
				});
		
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if(position<list.size()-1&&isShowDelete==false){
					/*就是点击C语言的那个按钮
					 * 点击这里是从第一个界面跳转到第二个界面
					 */
					CourseActivity.setBackChoice(1);//控制界面跳转的逻辑
					Intent i =new Intent(getActivity(),CourseActivity.class);
					startActivity(i);
					
				}
				
				
				if(position==list.size()-1&&isShowDelete==false){
					switch(index){
					case 0:mAdapter.add(new CourseCellDataProvider("Php", R.drawable.php));index++;break;
					case 1:mAdapter.add(new CourseCellDataProvider("Html", R.drawable.html1));index++;break;
					case 2:mAdapter.add(new CourseCellDataProvider("Ps", R.drawable.ps1));index++;break;
					case 3:mAdapter.add(new CourseCellDataProvider("网络操作", R.drawable.wlcz1));index++;break;
					case 4:mAdapter.add(new CourseCellDataProvider("面向对象", R.drawable.sub1));index++;break;
					case 5:mAdapter.add(new CourseCellDataProvider("计算机基础", R.drawable.jsj1));index=0;break;
                     default :break;
					}Toast.makeText(getActivity(),"可点击"+position,Toast.LENGTH_SHORT).show();
					mAdapter.notifyDataSetChanged();
				}
				if(position==list.size()-1&&isShowDelete==true){
					Toast.makeText(getActivity(),"你选择了:"+mAdapter.checkBox_chosen.toString(), Toast.LENGTH_SHORT).show();
				}
			
			}
		});
		 

//	   @Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		  int groupId=0; 
//		  int order=Menu.NONE;
//		   menu.add(groupId, itemId, order, "选择删除课程");
//		return super.onCreateOptionsMenu(menu);
//	}
//	@Override
//	public boolean onOptionsItemSelected(MenuItem item) {
//		switch(item.getItemId()){
//		case itemId:
//			Toast.makeText(getActivity(),"你选择了"+mAdapter.checkBox_chosen.toString(), Toast.LENGTH_SHORT).show();
//		}
//		return super.onOptionsItemSelected(item);
		

//		rootView.findViewById(R.id.C_Language).setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				MainActivityTwo.setBackChoice(1);
//				Toast.makeText(getActivity(), "C语言入门", Toast.LENGTH_SHORT).show();
//				Intent i =new Intent(getActivity(),MainActivityTwo.class);
//				startActivity(i);
//				
//			}
//
//		});
//		rootView.findViewById(R.id.php).setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				Toast.makeText(getActivity(), "PHP入门", Toast.LENGTH_SHORT).show();
//			}
//
//		});
//		//添加课程
//		rootView.findViewById(R.id.add).setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				Toast.makeText(getActivity(), "添加课程", Toast.LENGTH_SHORT).show();
//				MainActivity.setBackChoice(1);
//			//	getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.main_show, new IndexableListViewFragment()).commit();
//				getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.main_show, new MainFragment()).commit();
//			}
//		});
		

		return rootView;
	}


}
